package com.tim.app.ui.adapter.selceted;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListPopupWindow;
import android.widget.TextView;

import com.application.library.runtime.event.EventManager;
import com.application.library.util.TimeUtil;
import com.application.library.widget.RatioImageView;
import com.application.library.widget.recycle.BaseRecyclerAdapter;
import com.application.library.widget.roundimg.RoundedImageView;
import com.tim.app.R;
import com.tim.app.constant.EventTag;
import com.tim.app.server.entry.Answer;
import com.tim.app.ui.activity.some_homepage.SomeoneHomepageActivity;
import com.tim.app.util.BitmapLoader;

import java.util.List;


/**
 * 答案
 */
public class AnswerAdapter extends BaseRecyclerAdapter<BaseRecyclerAdapter.BaseRecyclerViewHolder, Answer> {

    private Context mContext;


    public AnswerAdapter(Context mContext, List<Answer> mDataList) {
        super(mDataList);
        this.mContext = mContext;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseRecyclerViewHolder holder = null;
        holder = new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_answer, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(BaseRecyclerViewHolder mHolder, int position, Answer data) {
        if (data == null) {
            return;
        }
        final ViewHolder holder = (ViewHolder) mHolder;
        if (null != data.getUserMsg() && !TextUtils.isEmpty(data.getUserMsg().getImage())) {
            BitmapLoader.ins().loadImage(data.getUserMsg().getImage(), holder.rivAvatar);
        }

        if (null != data.getUserMsg() && !TextUtils.isEmpty(data.getUserMsg().getName())) {
            holder.tvUserName.setText(data.getUserMsg().getName());
        }
        if (!TextUtils.isEmpty(data.getContent())) {
            holder.tvAnswerDesc.setText(data.getContent());
        }
        holder.tvPriseNumber.setText(mContext.getString(R.string.tvPriseNumber, String.valueOf(data.getAgreeCount())));
        holder.tvCommentNumber.setText(mContext.getString(R.string.tvCommentNumber, String.valueOf(data.getCommentCount())));
        holder.tvPublishTime.setText(mContext.getString(R.string.point, TimeUtil.formatCircleTime(data.getCreateTime())));

        if (null != data.getImageUrl()) {
            List<String> dataImageUrl = data.getImageUrl();
            holder.llImageContainer.setVisibility(View.VISIBLE);
            if (data.getImageUrl().size() == 1) {
                holder.llMoreImageContainer.setVisibility(View.GONE);
                holder.rivCover.setVisibility(View.VISIBLE);
                BitmapLoader.ins().loadImage(dataImageUrl.get(0), holder.rivCover);
            } else {
                holder.llMoreImageContainer.setVisibility(View.VISIBLE);
                holder.rivCover.setVisibility(View.GONE);
                BitmapLoader.ins().loadImage(dataImageUrl.get(0), holder.rivCoverOne);
                if (dataImageUrl.size() > 1) {
                    BitmapLoader.ins().loadImage(dataImageUrl.get(1), holder.rivCoverTwo);
                }
                if (dataImageUrl.size() > 2) {
                    BitmapLoader.ins().loadImage(dataImageUrl.get(2), holder.rivCoverThree);
                }
            }
        } else {
            holder.llImageContainer.setVisibility(View.GONE);
        }

        holder.tvUserName.setTag(data);
        holder.rivAvatar.setTag(data);
        holder.tvUserName.setOnClickListener(onClickListener);
        holder.rivAvatar.setOnClickListener(onClickListener);
        if (position == 0) {
            holder.tvOrderBy.setVisibility(View.VISIBLE);
        } else {
            holder.tvOrderBy.setVisibility(View.GONE);
        }
        holder.tvOrderBy.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                showListPopup(holder.tvOrderBy);
            }
        });
    }


    ListPopupWindow listPopupWindow;

    public void showListPopup(final TextView textView) {
        if (null == listPopupWindow) {
            final String items[] = {"最新排序", "最热排序"};
            listPopupWindow = new ListPopupWindow(mContext);

            ArrayAdapter arrayAdapter = new ArrayAdapter<String>(mContext, R.layout.pop_order_list_item, items);
            //设置ListView类型的适配器
            listPopupWindow.setAdapter(arrayAdapter);

            //给每个item设置监听事件
            listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    if (position == 0) {
                        textView.setText("最新");
                        EventManager.ins().sendEvent(EventTag.ORDER_BY_TIME, 0, 0, null);
                    } else {
                        textView.setText("最热");
                        EventManager.ins().sendEvent(EventTag.ORDER_BY_QUALITY, 0, 0, null);
                    }
                    listPopupWindow.dismiss();
                }
            });

            //设置ListPopupWindow的锚点,也就是弹出框的位置是相对当前参数View的位置来显示，
            listPopupWindow.setAnchorView(textView);

            //ListPopupWindow 距锚点的距离，也就是相对锚点View的位置
            listPopupWindow.setHorizontalOffset(0);
            listPopupWindow.setVerticalOffset(-textView.getHeight());

            //设置对话框的宽高
            listPopupWindow.setWidth(300);
            listPopupWindow.setHeight(300);
            listPopupWindow.setModal(false);

        }
        listPopupWindow.show();
    }


    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Answer answer = (Answer) v.getTag();
            SomeoneHomepageActivity.start(mContext, answer.getUserMsg(), false);
        }
    };

    public class ViewHolder extends BaseRecyclerViewHolder {

        LinearLayout llUserInfo;
        RoundedImageView rivAvatar;
        TextView tvUserName, tvAnswerDesc, tvPriseNumber, tvCommentNumber, tvPublishTime;
        TextView tvOrderBy;
        LinearLayout llImageContainer;
        RatioImageView rivCover;
        LinearLayout llMoreImageContainer;
        RatioImageView rivCoverOne;
        RatioImageView rivCoverTwo;
        RatioImageView rivCoverThree;


        public ViewHolder(View itemView) {
            super(itemView);
            llUserInfo = (LinearLayout) itemView.findViewById(R.id.llUserInfo);
            rivAvatar = (RoundedImageView) itemView.findViewById(R.id.rivAvatar);
            tvUserName = (TextView) itemView.findViewById(R.id.tvUserName);
            tvAnswerDesc = (TextView) itemView.findViewById(R.id.tvAnswerDesc);
            tvPriseNumber = (TextView) itemView.findViewById(R.id.tvPriseNumber);
            tvCommentNumber = (TextView) itemView.findViewById(R.id.tvCommentNumber);
            tvPublishTime = (TextView) itemView.findViewById(R.id.tvPublishTime);
            tvOrderBy = (TextView) itemView.findViewById(R.id.tvOrderBy);
            llImageContainer = (LinearLayout) itemView.findViewById(R.id.llImageContainer);
            rivCover = (RatioImageView) itemView.findViewById(R.id.rivCover);
            llMoreImageContainer = (LinearLayout) itemView.findViewById(R.id.llMoreImageContainer);
            rivCoverOne = (RatioImageView) itemView.findViewById(R.id.rivCoverOne);
            rivCoverTwo = (RatioImageView) itemView.findViewById(R.id.rivCoverTwo);
            rivCoverThree = (RatioImageView) itemView.findViewById(R.id.rivCoverThree);
        }

    }

}
